<script lang="ts" setup>
import { onMounted, reactive, ref } from 'vue';

import {
  useVbenVxeGrid,
  type VxeGridListeners,
  type VxeGridProps,
} from '#/adapter/vxe-table';
import { kickoutByTokenValue, onlineDevice } from '#/api/system/user';
import { TableAction } from '#/components/table-action';

const gridOptions: VxeGridProps<any> = {
  toolbarConfig: {
    refresh: true, // 刷新
    print: false, // 打印
    export: false, // 导出
    custom: true, // 自定义列
    zoom: true, // 最大化最小化
    slots: {
      buttons: 'toolbar-buttons',
    },
  },
  columns: [
    { field: 'user_name', title: '登录账号' },
    { field: 'os', title: '登录设备' },
    { field: 'browser', title: '浏览器' },
    { field: 'ip', title: '登录IP' },
    { field: 'create_time', title: '登录时间' },
    {
      width: 110,
      title: '操作',
      align: 'center',
      slots: { default: 'ACTION' },
      fixed: 'right',
    },
  ],
  data: [],
  height: '450',
  keepSource: true,
  pagerConfig: {
    pageSize: 5,
  },
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues) => {
        return await onlineDevice({
          page: page.currentPage,
          limit: page.pageSize,
          ...formValues,
        });
      },
    },
  },
};

const gridEvents: VxeGridListeners<any> = {};
const [Grid, gridApi] = useVbenVxeGrid({
  gridOptions,
  gridEvents,
});


const handleDelete = (row: any) => {
  kickoutByTokenValue({
    data: [row.key],
  }).then(() => {
    gridApi.reload();
  });
};

</script>
<template>
  <div>
    <Grid>
      <template #toolbar-buttons> 我的在线设备 </template>
      <template #ACTION="{ row }">
        <TableAction
          :actions="[
            {
              label: '强制下线',
              popConfirm: {
                title: '确定踢下线吗？',
                confirm: handleDelete.bind(null, row),
              },
            },
          ]"
        />
      </template>
    </Grid>
  </div>
</template>
<style lang="less" scoped></style>
